//
// Created by v on 2019/10/23 下午4:24.
// Copyright (c) 2019 jmdvirus All rights reserved.
//
//     ******          *             *--*
//       ||            *             |
//       ||  *--*--*---*  * *-********--*
//    * //   *  *  *   *\ / ||  |  |    |
//     **    *  *  *---* *  *   *--* *--*
//

#ifndef CUTEDEVICE_CMTASKRUNNER_H
#define CUTEDEVICE_CMTASKRUNNER_H

#include <iostream>
#include <vector>

#include "CMLock.h"

/**
 * Inherit from CMTaskRunnerInterface, implement run() function
 * When you want to run something, call registerTask(this)
 * TODO: add time to control
 */

class CMTaskRunnerInterface
{
  public:
    explicit CMTaskRunnerInterface(const char *name){};
    virtual ~CMTaskRunnerInterface(){};

    virtual int run() = 0;

    std::string m_name;
};

class CMTaskRunner
{
  public:
    CMTaskRunner();
    ~CMTaskRunner();

    void exit() { m_exit = true; }

    void registerTask(CMTaskRunnerInterface *task);

    int createTaskRunner();

  protected:
    friend void *thread_run(void *args);

  private:
    bool                                 m_exit;
    CMLock                               m_lock;
    std::vector<CMTaskRunnerInterface *> m_tmpTasks;
    std::vector<CMTaskRunnerInterface *> m_tasks;
};

#endif // CUTEDEVICE_CMTASKRUNNER_H
